package com.caipin.service.impl;

/**
 * @Author: zhunian0322@163.com
 * @DateTime: 2021-10-06 12:06
 * @Description: TODO
 */

@Service
public class AISecurityHomePageManagementServiceImpl implements AISecurityHomePageManagementService {
    @Autowired
    private SecurityFaceDao securityFaceDao;

    @Autowired
    private SecurityImageDao securityImageDao;

    @Autowired
    private GateAttributeDao gateAttributeDao;

    @Autowired
    private SecurityWarningDao securityWarningDao;

    @Autowired
    private SecurityPedestrianDao securityPedestrianDao;

    @Autowired
    private GateComparisonResultDao gateComparisonResultDao;

    public List<StrangerWarning> getStrangerWarningList(int count) {
        QueryWrapper<SecurityFace> queryWrapper = new QueryWrapper();
        queryWrapper.eq("mark", Boolean.valueOf(false));
        queryWrapper.eq("face_stranger", Boolean.valueOf(true));
        queryWrapper.ne("image_id", Integer.valueOf(0));
        queryWrapper.orderByDesc("snap_time");
        queryWrapper.last("limit " + count);
        List<SecurityFace> securityFaceList = this.securityFaceDao.selectList((Wrapper)queryWrapper);
        List<StrangerWarning> strangerWarningList = new ArrayList<>();
        for (SecurityFace securityFace : securityFaceList) {
            SecurityImage securityImage = (SecurityImage)this.securityImageDao.selectById(securityFace.getImageId());
            strangerWarningList.add(new StrangerWarning(securityFace

                    .getId(), securityFace
                    .getMark(), securityFace
                    .getSnapTime(), securityImage
                    .getLocation(), securityImage
                    .getImageSrc().replaceFirst("^((https|http|ftp|rtsp|mms)?:\\/\\/)[^\\s]+:[0-9]+", "")));
        }
        return strangerWarningList;
    }

    public List<CrowdWarning> getCrowdWarning(int count) {
        QueryWrapper<SecurityPedestrian> queryWrapper = new QueryWrapper();
        ((QueryWrapper)((QueryWrapper)((QueryWrapper)queryWrapper.eq("mark", Boolean.valueOf(false))).ne("image_id", Integer.valueOf(0))).orderByDesc("create_time")).last("LIMIT " + count);
        List<SecurityPedestrian> securityPedestrianList = this.securityPedestrianDao.selectList((Wrapper)queryWrapper);
        List<CrowdWarning> crowdWarningList = new ArrayList<>();
        for (SecurityPedestrian securityPedestrian : securityPedestrianList) {
            SecurityImage securityImage = (SecurityImage)this.securityImageDao.selectById(securityPedestrian.getImageId());
            crowdWarningList.add((new CrowdWarning())
                    .setImageSrc(securityImage.getImageSrc().replaceFirst("^((https|http|ftp|rtsp|mms)?:\\/\\/)[^\\s]+:[0-9]+", "")));
        }
        return crowdWarningList;
    }

    public List<EventStatistics> getEventStatisticsForWeek() {
        List<EventStatistics> eventStatisticsList = new ArrayList<>();
        List<EventStatistics> strangerList = getStrangerOfTheWeek();
        eventStatisticsList.addAll(strangerList);
        return eventStatisticsList;
    }

    private List<EventStatistics> getStrangerOfTheWeek() {
        LocalDate now = LocalDate.now();
        long second = now.atStartOfDay().toEpochSecond(ZoneOffset.of("+8"));
        List<Long> secondList = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            long diff = 86400L;
            secondList.add(Long.valueOf(second - diff * i));
        }
        QueryWrapper<SecurityFace> queryWrapper = new QueryWrapper();
        queryWrapper.select(new String[] { "DATE(create_time) days", "count(0) count" }).groupBy("days");
        List<Map<String, Object>> list = this.securityFaceDao.selectMaps((Wrapper)queryWrapper);
        List<EventStatistics> eventStatisticsList = new ArrayList<>();
        EventStatistics eventStatistics = (new EventStatistics()).setEventName(").setNumber(Long.valueOf(0L));
        for (Map<String, Object> map : list) {
            Date day = (Date)map.get("days");
            long l = day.getTime() / 1000L;
            for (Long s : secondList) {
                if (l == s.longValue())
                    eventStatistics.setNumber(Long.valueOf(eventStatistics.getNumber().longValue() + ((Long)map.get("count")).longValue()));
            }
        }
        eventStatisticsList.add(eventStatistics);
        return eventStatisticsList;
    }

    public List<EventStatistics> getEventStatisticsForDay() {
        List<EventStatistics> eventStatisticsList = new ArrayList<>();
        EventStatistics stranger = getStrangerOfTheDay();
        eventStatisticsList.add(stranger);
        return eventStatisticsList;
    }

    public List<HistoricalWarning> getHistoricalWarning(int count) {
        QueryWrapper<SecurityWarning> queryWrapper = new QueryWrapper();
        ((QueryWrapper)queryWrapper.orderByDesc("create_time"))
                .last("LIMIT " + count);
        List<SecurityWarning> securityWarningList = this.securityWarningDao.selectList((Wrapper)queryWrapper);
        List<HistoricalWarning> historicalWarningList = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (SecurityWarning securityWarning : securityWarningList)
            historicalWarningList.add((new HistoricalWarning())

                    .setTime(simpleDateFormat.format(securityWarning.getCreateTime()))
                    .setAddress(securityWarning.getAddress())
                    .setEvent(securityWarning.getEvent()));
        return historicalWarningList;
    }

    public List<InAndOutInfo> getNumberOfPeopleTakingIn(String startDate, String endDate) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date start = null;
        Date end = null;
        try {
            start = simpleDateFormat.parse(startDate);
            end = simpleDateFormat.parse(endDate);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select(new String[] { "COUNT(0) as c", "DATE(create_time) as date" });
        queryWrapper.ge("create_time", start);
        queryWrapper.le("create_time", end);
        queryWrapper.groupBy("DATE(create_time)");
        List list = this.gateAttributeDao.selectMaps((Wrapper)queryWrapper);
        List<InAndOutInfo> inAndOutInfoList = new ArrayList<>();
        for (Object o : list) {
            Map<String, Object> map = (Map<String, Object>)o;
            inAndOutInfoList.add((new InAndOutInfo())

                    .setOut(0L)
                    .setIn(((Long)map.get("c")).longValue())
                    .setDate((Date)map.get("date")));
        }
        return inAndOutInfoList;
    }

    public TemperatureInfo2 getNumberOfPeopleTakingTemperature() {
        QueryWrapper<GateComparisonResult> queryWrapper = new QueryWrapper();
        ((QueryWrapper)queryWrapper.between("screen_time", getThatDayStart(), getThatDayEnd()))
                .eq("user_type", "student");
        List<GateComparisonResult> gateComparisonResults = this.gateComparisonResultDao.selectList((Wrapper)queryWrapper);
        Map<String, TemperatureInfo2.Bean> map = new HashMap<>();
        for (GateComparisonResult gateComparisonResult : gateComparisonResults) {
            String grade = gateComparisonResult.getGrade();
            TemperatureInfo2.Bean bean = map.get(grade);
            if (bean == null) {
                TemperatureInfo2.Bean value = new TemperatureInfo2.Bean();
                value.setGrade(grade);
                if (Double.parseDouble(gateComparisonResult.getTemperature()) > 37.3D) {
                    value.setAbnormal(1);
                } else {
                    value.setNormal(1);
                }
                value.setTotal(value.getAbnormal() + value.getNormal());
                map.put(grade, value);
                continue;
            }
            if (Double.parseDouble(gateComparisonResult.getTemperature()) > 37.3D) {
                bean.setAbnormal(bean.getAbnormal() + 1);
            } else {
                bean.setNormal(bean.getNormal() + 1);
            }
            bean.setTotal(bean.getAbnormal() + bean.getNormal());
            map.put(grade, bean);
        }
        TemperatureInfo2 temperatureInfo2 = new TemperatureInfo2();
        List<TemperatureInfo2.Bean> beanList = new ArrayList<>();
        for (Map.Entry<String, TemperatureInfo2.Bean> entry : map.entrySet()) {
            TemperatureInfo2.Bean value = entry.getValue();
            beanList.add((new TemperatureInfo2.Bean())
                    .setGrade(value.getGrade())
                    .setNormal(value.getNormal())
                    .setAbnormal(value.getAbnormal())
                    .setTotal(value.getAbnormal() + value.getNormal()));
            temperatureInfo2.setAbnormal(temperatureInfo2.getAbnormal() + value.getAbnormal())
                    .setNormal(temperatureInfo2.getNormal() + value.getNormal())
                    .setTotal(temperatureInfo2.getAbnormal() + temperatureInfo2.getNormal());
        }
        temperatureInfo2.setBeanList(beanList);
        return temperatureInfo2;
    }

    private Date getThatDayStart() {
        Date date = new Date();
        date.setHours(0);
        date.setMinutes(0);
        date.setSeconds(0);
        return date;
    }

    private Date getThatDayEnd() {
        Date date = new Date();
        date.setHours(23);
        date.setMinutes(59);
        date.setSeconds(59);
        return date;
    }

    private EventStatistics getStrangerOfTheDay() {
        QueryWrapper<SecurityFace> queryWrapper = new QueryWrapper();
        queryWrapper.select(new String[] { "count(0) count" }).between("create_time", getThatDayStart(), getThatDayEnd());
        List<Map<String, Object>> list = this.securityFaceDao.selectMaps((Wrapper)queryWrapper);
        EventStatistics eventStatistics = (new EventStatistics()).setEventName(").setNumber(Long.valueOf(0L));
        for (Map<String, Object> map : list)
            eventStatistics = (new EventStatistics()).setEventName(").setNumber((Long)map.get("count"));
        return eventStatistics;
    }
}
